package algocasts;

public class P114_JewelsAndStones {
    // Time:O(m*n)  Space: O(1)
    public int numJewelsInStones(String jewels, String stones) {
        if (jewels == null || stones == null || jewels.length() == 0 || stones.length() == 0)
            return 0;
        int cnt = 0;
        for (int j = 0; j < jewels.length(); j++)
            for (int s = 0; s < stones.length(); s++)
                if (stones.charAt(s) == jewels.charAt(j)) cnt++;

        return cnt;
    }

    // Time: O(m+n)  Space: O(k)
    public int numJewelsInStones_HashMap(String jewels, String stones) {
        if (jewels == null || stones == null || jewels.length() == 0 || stones.length() == 0)
            return 0;
        boolean[] d = new boolean[256];
        int cnt = 0;
        for (int i = 0; i < jewels.length(); i++)
            d[jewels.charAt(i)] = true;
        for (int i = 0; i < stones.length(); i++)
            if (d[stones.charAt(i)]) ++cnt;
        return cnt;
    }
}
